<?php
namespace app\news\controller;
use app\news\model\ArticleCatM;
use app\rbac\model\AdminM;
use app\news\model\ArticleM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Article extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new ArticleM();
            $res = $model->add($data);
            if($res)
            {
                $this->success('新增成功',url('news/Article/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }

        //分类
        $catModel = new ArticleCatM();
        $catData = $catModel->getCatTree();
        $this->assign(array(
            '_page_title'=>'添加文章',
            '_page_title_english'=>'Article add',
            '_btn_title'=>'文章列表',
            '_btn_link'=>'lst',
            '_page_ico'=>'iconfa-book',
            'catData'=>$catData,
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $adminModel = new AdminM();
        $res = $adminModel->isAuth($id,'news_article');
        if($res === false)
        {
            $this->error('没有权限!');
        }
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new ArticleM();
            $res = $model->edit($data);
            if($res)
            {
                $this->success('修改成功',url('news/Article/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        //获取数据
        $data = Db::table('news_article')->where(['id'=>$id,'is_usable'=>1])->field('id,title,cat_id,content,intro,sm_logo,keywords,sort_num,is_show')->find();
        //未发布的文章才可以编辑
        if($data['is_show'] == 1)
        {
            $this->error('文章已经发布,请先撤销再编辑');
        }

        //分类
        $catModel = new ArticleCatM();
        $catData = $catModel->getCatTree();

        $this->assign(array(
            '_page_title'=>'编辑文章',
            '_page_title_english'=>'Article edit',
            '_btn_title'=>'文章列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-book',
            'data'=>$data,
            'catData'=>$catData,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        $model = new ArticleM();
        $data = $model->lst(15);
        // 获取分页显示
        $page = $data->render();
        $this->assign(array(
            '_page_title'=>'文章列表',
            '_page_title_english'=>'Article list',
            '_btn_title'=>'添加文章',
            '_btn_link'=>url('add'),
            '_page_ico'=>'iconfa-book',
            'data'=>$data,
            'page'=>$page,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $id = $this->request->post('id');
        $res = $adminModel->isAuth($id,'news_article');
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $model = new ArticleM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }

    /**
     * 发布/撤销
     * @param $id
     */
    public function publish()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $id = $this->request->post('id');
        $res = $adminModel->isAuth($id,'news_article');
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $model = new ArticleM();
        $model->publish($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }

    /**
     * 审核
     * @param $id
     */
    public function review()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $id = $this->request->post('id');
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $model = new ArticleM();
        $model->review($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }

}
